package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService catalogService;

    @Autowired
    private IStrategyCommentService commentService;

    @GetMapping
    public PageInfo queryForList(StrategyQueryObject qo){
        return strategyService.queryForList(qo);
    }

    @GetMapping("{id}")
    public Strategy getById(@PathVariable Long id){
        return strategyService.getById(id);
    }

    @GetMapping("{strategyId}/catalogs")
    public List<StrategyCatalog> getCatalogByStrategyId(@PathVariable Long strategyId){
        List<StrategyCatalog> list = catalogService.listByStrategyId(strategyId);
        return list;
    }

    @GetMapping("{strategyId}/comments")
    public PageInfo queryComments(StrategyCommentQueryObject qo){
        qo.setOrderby("sc.createTime desc");
        return commentService.queryForList(qo);
    }

    @PostMapping("{strategy.id}/comments")
    public JSONResult saveComment(StrategyComment comment,String[] tags){
        comment.setUser(UserContext.getCurrentUser());
        commentService.saveOrUpdate(comment,tags);
        return new JSONResult();
    }
}
